<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="/lib/layui-v2.5.5/css/layui.css"  media="all">
  <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
          
              
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
  <legend>种类修改</legend>
</fieldset>
 
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
    <label class="layui-form-label">种类名称</label>
    <div class="layui-input-block">
      <input type="text" name="name" id='name' lay-verify="required" lay-reqtext="种类名称不能为空" placeholder="请输入种类名称" autocomplete="off" class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">级别</label>
    <div class="layui-input-block">
      <input type="radio" name="level" value="LV1" title="LV1" lay-filter="level" checked="">
      <input type="radio" name="level" value="LV2" title="LV2" lay-filter="level">
    </div>
  </div>
  <div class="layui-form-item" id="parent" style="display:none">
    <label class="layui-form-label">父类</label>
    <div class="layui-input-block">
        <select type="text" id="parentid" class="layui-input" value=""lay-search="" lay-filter="parentid"></select>
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label">排序</label>
      <div class="layui-input-inline">
        <input type="number" name="sort" id='sort' lay-verify="required|number" autocomplete="off" class="layui-input">
      </div>
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">图片链接</label>
    <div class="layui-input-block">
        <input type="tel" name="imageurl" id='imageurl' lay-verify="required|url" autocomplete="off" class="layui-input" readonly>
        <button type="button" class="layui-btn" id="test1">上传图片</button>
    </div>
  </div>
  
  <div class="layui-form-item" pane="">
    <label class="layui-form-label">状态</label>
    <div class="layui-input-block">
        <input type="radio" name="status" value="1" title="有效" checked="">
        <input type="radio" name="status" value="0" title="无效">
    </div>
</div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button type="submit" class="layui-btn layui-btn-radius" lay-submit="" lay-filter="demo1">立即提交</button>
      <!-- <button type="reset" class="layui-btn layui-btn-primary">重置</button> -->
      <button class="layui-btn layui-btn-radius layui-btn-primary"  id="backBtn">返回</button>
    </div>
  </div>
</div>

          
<script src="/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/js/custorm/init.js" charset="utf-8"></script>

<script>
layui.use(['form', 'layedit', 'laydate','layedit','upload'], function(){
  var form = layui.form
  ,$ = layui.jquery
  ,layer = layui.layer
  ,layedit = layui.layedit
  ,laydate = layui.laydate,upload = layui.upload;
  var goodstype={'id':0,'name':'','parentid':0,'imageurl':'','level':'','status':true,'sort':0};  
  //$("#parent").hide();  
  var content = window.location.href;
  var id = content.split('=')[content.split('=').length - 1];
  AxajGet('../../api/shop/GetGoodsTypeById?id='+id,'../../login.html',initForm);
  AxajGet('../../api/shop/GetAllGoodsTypeWhereLV1','../../login.html',initParentSelect);

  ///表单赋值
function initForm(data){
  $('#name').val(data.name); 
  $('#imageurl').val(data.imageurl); 
  $('#sort').val(data.sort); 
  $("input[name=status][value='1']").attr("checked", data.status == true ? true : false);
  $("input[name=status][value='0']").attr("checked", data.status == false ? true : false);

  $("input[name=level][value='LV1']").attr("checked", data.level == 'LV1' ? true : false);
  $("input[name=level][value='LV2']").attr("checked", data.level == 'LV2' ? true : false);

  if(data.level=='LV2'){
    goodstype.parentid=data.parentid;
    $("#parent").show();
    $("#parentid").attr("lay-verify","required");
  }
}
    var uploadInst = upload.render({
    elem: '#test1'
    ,url: '../../api/sys/UpdateFile' 
    ,before: function(obj){
      obj.preview(function(index, file, result){
        $('#demo1').attr('src', result); 
      });
    }
    ,done: function(res){
      //如果上传失败
      if(res.resultCode ==0){
        $('#imageurl').val(res.data.url);
        return layer.msg('上传成功');
      }else{
        return layer.msg('上传失败：'+res.resultMsg);
      }
      //上传成功
    }
    ,error: function(){
        return layer.msg('上传失败');
    }
  });
  //监听提交
  form.on('submit(demo1)', function(data){
    goodstype=data.field;
    goodstype.sort=parseInt(data.field.sort);
    goodstype.status=data.field.status=='1'?true:false;
    goodstype.id=parseInt(id) ;

    if(goodstype.level=='LV1'){
        goodstype.parentid=0;
    }else{
        goodstype.parentid=parseInt($("#parentid").val()) ;
    }
    AxajPost('../../api/shop/UpdateGoodsType',goodstype,'保存成功','../../login.html');
    return;
  });
  $("#backBtn").click(function () {
        window.location.href="../goodstype.html";
   });
form.on('radio(level)', function(data){
    if(data.value=='LV1'){
        $("#parent").hide();
        $("#parentid").attr("lay-verify","");
    }else{
        $("#parent").show();
        $("#parentid").attr("lay-verify","required");
    }
});  
function initParentSelect(data){
    $('#parentid').html('');
    $("#parentid").append("<option></option>");
    for (var i = 0; i < data.length; i++) {
            if(data[i].id==goodstype.parentid){
              $("#parentid").append("<option value='"+data[i].id+"'  selected>" + data[i].name+ "</option>");
            }else{
              $("#parentid").append("<option value='"+data[i].id+"'>" + data[i].name+ "</option>");
            }
    }
    form.render();
}
///表单赋值
// form.val("demo1", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
//   "username": "贤心" // "name": "value"
//   ,"sex": "女"
//   ,"auth": 3
//   ,"check[write]": true
//   ,"open": false
//   ,"desc": "我爱layui"
// });

});
</script>

</body>
</html>